#! /usr/bin/python

import os
import pdfrw


INVOICE_TEMPLATE_PATH = 'invoice.pdf'
INVOICE_OUTPUT_PATH = 'e.pdf'

def to_female(input_pdf_path, output_pdf_path, which="pacg"):
    if which == "pacg":
        pacg_to_female(input_pdf_path, output_pdf_path)
    elif which == "apostl":
        apostl_to_female(input_pdf_path, output_pdf_path)

def pacg_to_female(input_pdf_path, output_pdf_path):
    ANNOT_KEY = '/Annots'
    ANNOT_FIELD_KEY = '/AS'
    ANNOT_VAL_KEY = '/V'
    ANNOT_RECT_KEY = '/Rect'
    SUBTYPE_KEY = '/Subtype'
    WIDGET_SUBTYPE_KEY = '/Widget'
    d = True
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))) 
    annotations = template_pdf.pages[0][ANNOT_KEY]
    i = 0
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                key = annotation['/Parent']['/T'][1:-1]
                #print(key)
                if key != 'Group14':
                    continue
                if i == 0:
                    annotation.update(pdfrw.PdfDict(AS=pdfrw.PdfString('/Off')))
                elif i == 1:
                    annotation.update(pdfrw.PdfDict(AS=pdfrw.PdfString('/Choice1')))
                i+=1

                if i == 2:
                    break
    i = 0
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation['/Parent']:
                key = annotation['/Parent']['/T'][1:-1]
                if key != 'Group14':
                    continue
                if i <= 3:
                    annotation['/Parent'].update(pdfrw.PdfDict(V=pdfrw.PdfString('/Choice1')))
                i+=1

    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)



def apostl_to_female(input_pdf_path, output_pdf_path):
    ANNOT_KEY = '/Annots'
    ANNOT_FIELD_KEY = '/AS'
    ANNOT_VAL_KEY = '/V'
    ANNOT_RECT_KEY = '/Rect'
    SUBTYPE_KEY = '/Subtype'
    WIDGET_SUBTYPE_KEY = '/Widget'
    d = True
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
    annotations = template_pdf.pages[0][ANNOT_KEY]
    i = 0
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                key = annotation['/Parent']['/T'][1:-1]
                #print(key, annotation[ANNOT_FIELD_KEY])
                if key != 'GENETIC SEX':
                    continue
                if i == 0:
                    annotation.update(pdfrw.PdfDict(AS=pdfrw.PdfString('/Off')))
                elif i == 1:
                    annotation.update(pdfrw.PdfDict(AS=pdfrw.PdfString('/FEMALE')))
                elif i == 2:
                    annotation.update(pdfrw.PdfDict(AS=pdfrw.PdfString('/Off')))
                i+=1
                if i == 3:
                    break
    i = 0
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation['/Parent']:
                key = annotation['/Parent']['/T'][1:-1]
                if key != 'GENETIC SEX':
                    continue
                if i <= 2:
                    annotation['/Parent'].update(pdfrw.PdfDict(V=pdfrw.PdfString('/FEMALE')))
                i+=1

    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)


if __name__ == '__main__':
    to_female(INVOICE_TEMPLATE_PATH, INVOICE_OUTPUT_PATH)
